

    const passport = require('koa-passport')
    const jwt = require('jsonwebtoken')
    const loginDao = require('../dao/loginDao')

    module.exports = {
        
        checkLogin: async (ctx, next) => {
            return passport.authenticate('local', async (err, user, info) => {
                if(err) {
                    ctx.body = {
                        code: -1,
                        msg: err
                    }
                }
                else {
                    if(!user) {
                        ctx.body = {
                            code: 1,
                            msg: info,
                        }
                    }
                    else {
                        let token = jwt.sign(user, '15914035518', {expiresIn: '1d', algorithm: 'HS256'})
                        user = {...user, password: ''}
                        ctx.body = {
                            code: 0,
                            msg: '登录成功',
                            user,
                            token: token
                        }
                    }
                }
                
            })(ctx, next)
        },

        register: async (ctx, next) => {
            let res = await loginDao.register(ctx.request.body)
            ctx.body =  {
                res: res
            }
        },

        testapi: async (ctx, next) => {
            ctx.body = {
                res: 'success'
            }
        }

    }